home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / termTimeDate.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  12KB  |  547 lines

  1. /*
  2. **    termTimeDate.c
  3. **
  4. **    Phone rate calculation support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. ** :ts=4
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* SelectTime():
  15.      *
  16.      *    Searches for the correct unit/pay conversion values.
  17.      */
  18.  
  19. VOID __regargs
  20. SelectTime(struct PhoneEntry *SomeEntry,struct List *List,struct timeval *TimeVal)
  21. {
  22.     struct TimeDateNode            *TimeDateNode;
  23.     struct DateStamp __aligned     Date;
  24.     struct ClockData             ClockData;
  25.     UBYTE                         Time;
  26.     BYTE                         FoundOne = FALSE;
  27.     LONG                         i,Last;
  28.  
  29.         /* If we get a specific time of day, use it. */
  30.  
  31.     if(TimeVal)
  32.         Amiga2Date(TimeVal -> tv_secs,&ClockData);
  33.     else
  34.     {
  35.             /* Obtain current time and date. */
  36.  
  37.         DateStamp(&Date);
  38.  
  39.             /* Convert into a more suitable form (note: seconds are
  40.              * required as an input value, ice is extra).
  41.              */
  42.  
  43.         Amiga2Date(Date . ds_Days * 86400 + Date . ds_Minute * 60 + Date . ds_Tick / TICKS_PER_SECOND,&ClockData);
  44.     }
  45.  
  46.         /* Apparently, in the US of A the week starts with Sunday, we'll
  47.          * wrap the week around to let it start with Monday.
  48.          */
  49.  
  50.     if(ClockData . wday)
  51.         ClockData . wday--;
  52.     else
  53.         ClockData . wday = 6;
  54.  
  55.         /* Change the month, too... */
  56.  
  57.     ClockData . month--;
  58.  
  59.     if(List)
  60.     {
  61.         if(!List -> lh_Head -> ln_Succ)
  62.             List = NULL;
  63.     }
  64.  
  65.         /* If no special list to search is given use the phonebook entry. */
  66.  
  67.     if(!List)
  68.     {
  69.         if(SomeEntry && !SomeEntry -> Header -> NoRates)
  70.             List = (struct List *)&SomeEntry -> TimeDateList;
  71.         else
  72.         {
  73.             PayPerUnit[DT_FIRST_UNIT]    = 0;
  74.             PayPerUnit[DT_NEXT_UNIT]    = 0;
  75.             SecPerUnit[DT_FIRST_UNIT]    = 0;
  76.             SecPerUnit[DT_NEXT_UNIT]    = 0;
  77.  
  78.             return;
  79.         }
  80.     }
  81.  
  82.     if(!List -> lh_Head -> ln_Succ)
  83.     {
  84.         PayPerUnit[DT_FIRST_UNIT]    = 0;
  85.         PayPerUnit[DT_NEXT_UNIT]    = 0;
  86.         SecPerUnit[DT_FIRST_UNIT]    = 0;
  87.         SecPerUnit[DT_NEXT_UNIT]    = 0;
  88.  
  89.         return;
  90.     }
  91.  
  92.         /* First step: search for current day. */
  93.  
  94.     TimeDateNode = (struct TimeDateNode *)List -> lh_Head;
  95.  
  96.         /* Skip first entry. */
  97.  
  98.     TimeDateNode = (struct TimeDateNode *)TimeDateNode -> VanillaNode . ln_Succ;
  99.  
  100.         /* First step: search for date settings. */
  101.  
  102.     while(TimeDateNode -> VanillaNode . ln_Succ)
  103.     {
  104.             /* Does it match a specific date? */
  105.  
  106.         if(TimeDateNode -> Header . Month == ClockData . month && TimeDateNode -> Header . Day == ClockData . mday)
  107.         {
  108.             FoundOne = TRUE;
  109.  
  110.             break;
  111.         }
  112.  
  113.         TimeDateNode = (struct TimeDateNode *)TimeDateNode -> VanillaNode . ln_Succ;
  114.     }
  115.  
  116.         /* Second step: search for week day settings. */
  117.  
  118.     if(!FoundOne)
  119.     {
  120.         TimeDateNode = (struct TimeDateNode *)List -> lh_Head;
  121.  
  122.             /* Skip first entry. */
  123.  
  124.         TimeDateNode = (struct TimeDateNode *)TimeDateNode -> VanillaNode . ln_Succ;
  125.  
  126.         while(TimeDateNode -> VanillaNode . ln_Succ)
  127.         {
  128.                 /* Does it match a specific day? */
  129.  
  130.             if(TimeDateNode -> Header . Month == -1 && (TimeDateNode -> Header . Day & (1L << ClockData . wday)))
  131.             {
  132.                 FoundOne = TRUE;
  133.  
  134.                 break;
  135.             }
  136.  
  137.             TimeDateNode = (struct TimeDateNode *)TimeDateNode -> VanillaNode . ln_Succ;
  138.         }
  139.     }
  140.  
  141.         /* Third step: use default settings. */
  142.  
  143.     if(!FoundOne)
  144.         TimeDateNode = (struct TimeDateNode *)List -> lh_Head;
  145.  
  146.         /* Convert current time to packed format. */
  147.  
  148.     Time = DT_GET_TIME(ClockData . hour,ClockData . min);
  149.  
  150.         /* Start with a blank. */
  151.  
  152.     Last = -1;
  153.  
  154.         /* Look for fitting time. */
  155.  
  156.     for(i = 0 ; i < TimeDateNode -> Table[0] . Count ; i++)
  157.     {
  158.             /* The time we are looking for must be >= the
  159.              * current time.
  160.              */
  161.  
  162.         if(TimeDateNode -> Table[i] . Time > Time)
  163.         {
  164.             if(i == 0)
  165.                 break;
  166.             else
  167.             {
  168.                 Last = i - 1;
  169.                 break;
  170.             }
  171.         }
  172.         else
  173.             continue;
  174.     }
  175.  
  176.         /* If none is found, take the last one in the list.
  177.          * Note that this requires the list to be sorted in
  178.          * ascending order.
  179.          */
  180.  
  181.     if(Last == -1)
  182.         Last = TimeDateNode -> Table[0] . Count - 1;
  183.  
  184.         /* Fill in the remaining data. */
  185.  
  186.     PayPerUnit[DT_FIRST_UNIT]    = TimeDateNode -> Table[Last] . PayPerUnit[DT_FIRST_UNIT];
  187.     PayPerUnit[DT_NEXT_UNIT]    = TimeDateNode -> Table[Last] . PayPerUnit[DT_NEXT_UNIT];
  188.     SecPerUnit[DT_FIRST_UNIT]    = TimeDateNode -> Table[Last] . SecPerUnit[DT_FIRST_UNIT];
  189.     SecPerUnit[DT_NEXT_UNIT]    = TimeDateNode -> Table[Last] . SecPerUnit[DT_NEXT_UNIT];
  190. }
  191.  
  192. struct List * __regargs
  193. FindTimeDate(struct List *Patterns,STRPTR Number)
  194. {
  195.     struct PatternNode    *Node;
  196.     UBYTE                 Dst[516];
  197.  
  198.     if(Kick30)
  199.     {
  200.         for(Node = (struct PatternNode *)Patterns -> lh_Head ; Node -> Node . ln_Succ ; Node = (struct PatternNode *)Node -> Node . ln_Succ)
  201.         {
  202.             if(ParsePatternNoCase(Node -> Pattern,Dst,516) != -1)
  203.             {
  204.                 if(MatchPatternNoCase(Dst,Number))
  205.                     return(&Node -> List);
  206.             }
  207.         }
  208.     }
  209.     else
  210.     {
  211.         UBYTE    Src[256];
  212.         STRPTR    Index;
  213.  
  214.         for(Node = (struct PatternNode *)Patterns -> lh_Head ; Node -> Node . ln_Succ ; Node = (struct PatternNode *)Node -> Node . ln_Succ)
  215.         {
  216.             strcpy(Index = Src,Node -> Pattern);
  217.  
  218.             while(*Index)
  219.             {
  220.                 *Index = ToUpper(*Index);
  221.  
  222.                 Index++;
  223.             }
  224.  
  225.             if(ParsePatternNoCase(Src,Dst,516) != -1)
  226.             {
  227.                 if(MatchPatternNoCase(Dst,Number))
  228.                     return(&Node -> List);
  229.             }
  230.         }
  231.     }
  232.  
  233.     return(NULL);
  234. }
  235.  
  236. VOID __regargs
  237. DeletePatternNode(struct PatternNode *Pattern)
  238. {
  239.     if(Pattern)
  240.     {
  241.         struct TimeDateNode    *Node,
  242.                             *NextNode;
  243.  
  244.         for(Node = (struct TimeDateNode *)Pattern -> List . lh_Head ; NextNode = (struct TimeDateNode *)Node -> VanillaNode . ln_Succ ; Node = NextNode)
  245.             FreeTimeDateNode(Node);
  246.  
  247.         FreeVecPooled(Pattern);
  248.     }
  249. }
  250.  
  251. struct PatternNode * __regargs
  252. CreatePatternNode(STRPTR Comment)
  253. {
  254.     struct PatternNode *Node;
  255.  
  256.     if(Node = (struct PatternNode *)AllocVecPooled(sizeof(struct PatternNode),MEMF_ANY | MEMF_CLEAR))
  257.     {
  258.         struct TimeDateNode *TimeDateNode;
  259.  
  260.         Node -> Node . ln_Name = Node -> Comment;
  261.  
  262.         strcpy(Node -> Comment,Comment);
  263.  
  264.         NewList(&Node -> List);
  265.  
  266.         if(TimeDateNode = CreateTimeDateNode(-1,-1,"",2))
  267.         {
  268.             AddTail(&Node -> List,&TimeDateNode -> VanillaNode);
  269.  
  270.             return(Node);
  271.         }
  272.  
  273.         FreeVecPooled(Node);
  274.     }
  275.  
  276.     return(NULL);
  277. }
  278.  
  279. VOID __regargs
  280. DeletePatternList(struct List *List)
  281. {
  282.     if(List)
  283.     {
  284.         struct PatternNode    *Pattern,
  285.                             *NextPattern;
  286.  
  287.         for(Pattern = (struct PatternNode *)List -> lh_Head ; NextPattern = (struct PatternNode *)Pattern -> Node . ln_Succ ; Pattern = NextPattern)
  288.             DeletePatternNode(Pattern);
  289.  
  290.         FreeVecPooled(List);
  291.     }
  292. }
  293.  
  294. struct List * __regargs
  295. LoadTimeDateList(STRPTR Name,LONG *Error)
  296. {
  297.     struct IFFHandle    *Handle;
  298.     struct List            *List = NULL;
  299.  
  300.     *Error = 0;
  301.  
  302.     if(Handle = (struct IFFHandle *)AllocIFF())
  303.     {
  304.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  305.         {
  306.             InitIFFasDOS(Handle);
  307.  
  308.             if(!(*Error = OpenIFF(Handle,IFFF_READ)))
  309.             {
  310.                 STATIC LONG Stops[3 * 2] =
  311.                 {
  312.                     ID_TERM, ID_VERS,
  313.                     ID_TERM, ID_NAME,
  314.                     ID_TERM, ID_DATE
  315.                 };
  316.  
  317.                 struct ContextNode *Chunk;
  318.  
  319.                 if(!(*Error = StopChunks(Handle,Stops,3)))
  320.                 {
  321.                     if(List = (struct List *)AllocVecPooled(sizeof(struct MinList),MEMF_ANY))
  322.                     {
  323.                         struct PatternNode *Pattern = NULL;
  324.  
  325.                         NewList(List);
  326.  
  327.                         while(!ParseIFF(Handle,IFFPARSE_SCAN))
  328.                         {
  329.                             Chunk = CurrentChunk(Handle);
  330.  
  331.                             if(Chunk -> cn_ID == ID_NAME)
  332.                             {
  333.                                 if(Pattern = (struct PatternNode *)AllocVecPooled(sizeof(struct PatternNode),MEMF_ANY | MEMF_CLEAR))
  334.                                 {
  335.                                     if(ReadChunkBytes(Handle,Pattern -> Pattern,Chunk -> cn_Size) == Chunk -> cn_Size)
  336.                                     {
  337.                                         NewList(&Pattern -> List);
  338.  
  339.                                         Pattern -> Node . ln_Name = Pattern -> Comment;
  340.  
  341.                                         AddTail(List,Pattern);
  342.                                     }
  343.                                     else
  344.                                     {
  345.                                         *Error = IoErr();
  346.  
  347.                                         FreeVecPooled(Pattern);
  348.  
  349.                                         break;
  350.                                     }
  351.                                 }
  352.                                 else
  353.                                 {
  354.                                     *Error = ERR_NO_MEM;
  355.  
  356.                                     break;
  357.                                 }
  358.                             }
  359.  
  360.                             if(Chunk -> cn_ID == ID_DATE)
  361.                             {
  362.                                 struct TimeDateNode    *Node;
  363.                                 WORD                 Count = (Chunk -> cn_Size - sizeof(struct TimeDateHeader)) / sizeof(struct TimeDate);
  364.  
  365.                                 if(!Pattern)
  366.                                 {
  367.                                     *Error = IFFERR_MANGLED;
  368.  
  369.                                     break;
  370.                                 }
  371.  
  372.                                 if(Node = CreateTimeDateNode(-1,-1,"",Count))
  373.                                 {
  374.                                     if(ReadChunkBytes(Handle,&Node -> Header,sizeof(struct TimeDateHeader)) == sizeof(struct TimeDateHeader))
  375.                                     {
  376.                                         if(ReadChunkRecords(Handle,Node -> Table,sizeof(struct TimeDate),Count) == Count)
  377.                                         {
  378.                                             AdaptTimeDateNode(Node);
  379.  
  380.                                             AddTail(&Pattern -> List,&Node -> VanillaNode);
  381.                                         }
  382.                                         else
  383.                                         {
  384.                                             *Error = IoErr();
  385.  
  386.                                             FreeTimeDateNode(Node);
  387.  
  388.                                             break;
  389.                                         }
  390.                                     }
  391.                                     else
  392.                                     {
  393.                                         *Error = IoErr();
  394.  
  395.                                         FreeTimeDateNode(Node);
  396.  
  397.                                         break;
  398.                                     }
  399.                                 }
  400.                             }
  401.                         }
  402.                     }
  403.                 }
  404.  
  405.                 CloseIFF(Handle);
  406.             }
  407.  
  408.             Close(Handle -> iff_Stream);
  409.         }
  410.         else
  411.             *Error = IoErr();
  412.  
  413.         FreeIFF(Handle);
  414.     }
  415.     else
  416.         *Error = ERR_NO_MEM;
  417.  
  418.     if(*Error)
  419.     {
  420.         DeletePatternList(List);
  421.  
  422.         return(NULL);
  423.     }
  424.     else
  425.         return(List);
  426. }
  427.  
  428. BOOL __regargs
  429. SaveTimeDateList(STRPTR Name,struct List *List,LONG *Error)
  430. {
  431.     *Error = 0;
  432.  
  433.     if(List -> lh_Head -> ln_Succ)
  434.     {
  435.         struct IFFHandle *Handle;
  436.  
  437.         if(Handle = (struct IFFHandle *)AllocIFF())
  438.         {
  439.             BOOL Created;
  440.  
  441.             if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
  442.             {
  443.                 Created = TRUE;
  444.  
  445.                 InitIFFasDOS(Handle);
  446.  
  447.                 if(!(*Error = OpenIFF(Handle,IFFF_WRITE)))
  448.                 {
  449.                     if(!(*Error = PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN)))
  450.                     {
  451.                         if(!(*Error = PushChunk(Handle,0,ID_VERS,IFFSIZE_UNKNOWN)))
  452.                         {
  453.                             struct TermInfo TermInfo;
  454.  
  455.                             TermInfo . Version    = CONFIG_FILE_VERSION;
  456.                             TermInfo . Revision    = CONFIG_FILE_REVISION;
  457.  
  458.                             if(WriteChunkRecords(Handle,&TermInfo,sizeof(struct TermInfo),1) == 1)
  459.                             {
  460.                                 if(!(*Error = PopChunk(Handle)))
  461.                                 {
  462.                                     struct PatternNode *Node;
  463.  
  464.                                     for(Node = (struct PatternNode *)List -> lh_Head ; !(*Error) && Node -> Node . ln_Succ ; Node = (struct PatternNode *)Node -> Node . ln_Succ)
  465.                                     {
  466.                                         if(!(*Error = PushChunk(Handle,0,ID_NAME,296)))
  467.                                         {
  468.                                             if(WriteChunkBytes(Handle,Node -> Pattern,296) == 296)
  469.                                             {
  470.                                                 if(!(*Error = PopChunk(Handle)))
  471.                                                 {
  472.                                                     struct TimeDateNode *TimeDateNode;
  473.  
  474.                                                     for(TimeDateNode = (struct TimeDateNode *)Node -> List . lh_Head ; TimeDateNode -> VanillaNode . ln_Succ ; TimeDateNode = (struct TimeDateNode *)TimeDateNode -> VanillaNode . ln_Succ)
  475.                                                     {
  476.                                                         if(!(*Error = PushChunk(Handle,0,ID_DATE,sizeof(struct TimeDateHeader) + TimeDateNode -> Table[0] . Count * sizeof(struct TimeDate))))
  477.                                                         {
  478.                                                             if(WriteChunkBytes(Handle,&TimeDateNode -> Header,sizeof(struct TimeDateHeader)) != sizeof(struct TimeDateHeader))
  479.                                                             {
  480.                                                                 *Error = IoErr();
  481.  
  482.                                                                 break;
  483.                                                             }
  484.                                                             else
  485.                                                             {
  486.                                                                 if(WriteChunkBytes(Handle,TimeDateNode -> Table,TimeDateNode -> Table[0] . Count * sizeof(struct TimeDate)) != TimeDateNode -> Table[0] . Count * sizeof(struct TimeDate))
  487.                                                                 {
  488.                                                                     *Error = IoErr();
  489.  
  490.                                                                     break;
  491.                                                                 }
  492.                                                                 else
  493.                                                                 {
  494.                                                                     if(*Error = PopChunk(Handle))
  495.                                                                         break;
  496.                                                                 }
  497.                                                             }
  498.                                                         }
  499.                                                         else
  500.                                                             break;
  501.                                                     }
  502.                                                 }
  503.                                             }
  504.                                             else
  505.                                                 *Error = IoErr();
  506.                                         }
  507.                                     }
  508.                                 }
  509.                             }
  510.                         }
  511.  
  512.                         if(!(*Error))
  513.                             *Error = PopChunk(Handle);
  514.                     }
  515.  
  516.                     CloseIFF(Handle);
  517.                 }
  518.  
  519.                 Close(Handle -> iff_Stream);
  520.             }
  521.             else
  522.             {
  523.                 *Error = IoErr();
  524.  
  525.                 Created = FALSE;
  526.             }
  527.  
  528.             if(Created)
  529.             {
  530.                 if(*Error)
  531.                     DeleteFile(Name);
  532.                 else
  533.                     AddProtection(Name,FIBF_EXECUTE);
  534.             }
  535.  
  536.             FreeIFF(Handle);
  537.         }
  538.         else
  539.             *Error = ERR_NO_MEM;
  540.     }
  541.  
  542.     if(*Error)
  543.         return(FALSE);
  544.     else
  545.         return(TRUE);
  546. }
  547.